using System;
using System.IO;

using NDesk.DBus;
using org.freedesktop.DBus;

namespace Dashboard
{
	// FIXME: This DBus interface sucks! It sends serialized
	// messages and then deserializes them in the engine.
    // Refactor this into a real D-Bus interface that can
    // be used from any application.

	[Interface ("org.gnome.Dashboard")]
	public interface IDBusTransport
	{
		void Send (byte[] message);
		//void Subscribe (string pattern, MessageHandler handler);
	}

    public class DBusConnection : Connection
    {
        private const string interface_name = "org.gnome.Dashboard";
        private const string path_name = "/org/gnome/Dashboard/Transport";

        IDBusTransport transport = null;

        static DBusConnection()
        {
            NDesk.DBus.BusG.Init();
        }

        public DBusConnection()
        {
            Bus bus = Bus.Session;

            transport = bus.GetObject<IDBusTransport>(interface_name, new ObjectPath(path_name));
        }

        public override void Send(Message message)
        {
            MemoryStream stream = new MemoryStream();
            Message.Serializer.Serialize(stream, message);

            transport.Send(stream.GetBuffer());
        }

        public override void Subscribe(string pattern, MessageHandler handler)
        {
            //transport.Subscribe (pattern, handler);
            throw new NotImplementedException();
        }
    }
}